package com.itheima.health.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class DateTimeUtil {
    /**
     * 获取当天的开始时间
     * @param date
     * @return
     */
    public static LocalDateTime getStartOfDay(LocalDate date) {
        return LocalDateTime.of(date, LocalTime.MIN);
    }

    /**
     * 获取当天的结束时间
     * @param date
     * @return
     */
    public static LocalDateTime getEndOfDay(LocalDate date) {
        return LocalDateTime.of(date, LocalTime.MAX);
    }

    /**
     * 获取n个月前的月末日期
     * @param date
     * @param month
     * @return
     */
    public static LocalDate getLastDateBeforeMonths(LocalDate date, Integer month) {
        return date.withDayOfMonth(1).minusDays(1)
                .minusMonths(month - 1);
    }

    /**
     * 获取n个月后的月末日期
     * @param date
     * @param month
     * @return
     */
    public static LocalDate getLastDateAfterMonths(LocalDate date, Integer month) {
        return date.withDayOfMonth(1).minusDays(1)
                .plusMonths(month + 1);
    }

    /**
     * 获取当月的月末日期
     * @param date
     * @return
     */
    public static LocalDate getLastDate(LocalDate date) {
        return date.withDayOfMonth(1).minusDays(1)
                .plusMonths(1);
    }

    public static String format(LocalDate date, String pattern){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return dateTimeFormatter.format(date);
    }

    public static LocalDate parse(String date, String pattern){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDate.parse(date, dateTimeFormatter);
    }
}
